﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Data.SqlClient;

namespace HotelManagement
{
    public partial class RoomPriceFRM : Form
    {
        SqlConnection Cnn = new SqlConnection(Globals.DBConnection());
        DataSet ds = new DataSet();
        SqlDataAdapter da;
        DataSet ds2 = new DataSet();
        SqlDataAdapter da2;
        int rp_id;

        public RoomPriceFRM()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DayPicker form = new DayPicker();
            form.ShowDialog();

            PersianCalendar PC = new PersianCalendar();
            DateTime day = new DateTime(form.getyear(), form.getmonth(), form.getday());
            string shamsidate =
                PC.GetYear(day).ToString() + "/" +
                PC.GetMonth(day).ToString() + "/" +
                PC.GetDayOfMonth(day).ToString();

            TimeTxt.Text = shamsidate;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DayPicker form = new DayPicker();
            form.ShowDialog();

            PersianCalendar PC = new PersianCalendar();
            DateTime day = new DateTime(form.getyear(), form.getmonth(), form.getday());
            string shamsidate =
                PC.GetYear(day).ToString() + "/" +
                PC.GetMonth(day).ToString() + "/" +
                PC.GetDayOfMonth(day).ToString();
        }

        private void RoomPriceFRM_Load(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select rp_id as [کد],rp_time [بازه زمانی],rp_price as [قیمت],rt_type_name as [نوع اتاق] from Room_Price,Room_Type where rp_rt_id=rt_id", Cnn);
            da.Fill(ds);
            DgViewRprice.DataSource = ds.Tables[0];
            
            da2 = new SqlDataAdapter("select rt_type_name,rt_id from Room_Type", Cnn);
            da2.Fill(ds2);
            comboBox1.DataSource = ds2.Tables[0];
            comboBox1.DisplayMember = "rt_type_name";
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(TimeTxt.Text) && !string.IsNullOrEmpty(comboBox1.Text) && !string.IsNullOrEmpty(PriceTxt.Text))
            {
                SqlCommand SaveINDB = new SqlCommand("INSERT INTO Room_Price (rp_time,rp_price,rp_rt_id) values(@time,@price,@rt_id)", Cnn);
                SaveINDB.Parameters.AddWithValue("@time", TimeTxt.Text.Trim());
                SaveINDB.Parameters.AddWithValue("@price", PriceTxt.Text.Trim());
                SaveINDB.Parameters.AddWithValue("@rt_id", (int)comboBox1.SelectedValue);

                Cnn.Open();
                SaveINDB.ExecuteNonQuery();
                Cnn.Close();

                ds.Clear();
                da.Fill(ds);
                DgViewRprice.DataSource = ds.Tables[0];
            }
            else
            {
                MessageBox.Show("لطفا همه قسمت ها را تکمیل نمایید");
            }
        }

        private void DeleteBtn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("آیا برای حذف  مطمئنید؟", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                SqlCommand DeleteCMD = new SqlCommand("DELETE FROM Room_Price WHERE rp_id=@rp_id", Cnn);
                DeleteCMD.Parameters.AddWithValue("@rp_id", rp_id);
                Cnn.Open();
                DeleteCMD.ExecuteNonQuery();
                Cnn.Close();

                ds.Clear();
                da.Fill(ds);
                DgViewRprice.DataSource = ds.Tables[0];

                DeleteBtn.Enabled = false;
                MessageBox.Show("حذف  با موفقیت انجام شد");

            }
        }

        private void DgViewRprice_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DeleteBtn.Enabled = true;
            rp_id = (int)DgViewRprice.CurrentRow.Cells[0].Value;
        }


    }
}
